﻿Public Class CtrlMenuApplication
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Request.Form("__EVENTTARGET") = CType(Me.Page, OpenDelibPage).Master.UpdatePanel.UniqueID Then
                ExecuterAssistant(Request.Form("__EVENTARGUMENT"))
            End If
        End If
    End Sub

    Public Function Initialiser(application As String) As String
        Dim collectionMenuPerso As List(Of ServiceMenu.Menu) = Nothing
        Dim libelleMenu As String = ""

        Using svc As New ServiceMenu.MenuPersonnelClient()
            Dim params As New ServiceMenu.ParametresRecupererMenuApplication
            params.Authentication = New ServiceMenu.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance().getToken()
            params.Application = application
            params.Utilisateur = CType(Me.Page, OpenDelibPage).Master.Utilisateur

            Dim res As ServiceMenu.RecupererMenuApplicationResult
            res = svc.RecupererMenuApplication(params)
            If res.OK = False Then
                Tools.NotifyErreur(Me.Page, res.ErrorMessage)
            ElseIf res.Menus.Count > 0 Then
                libelleMenu = res.Menus(0).Libelle

                For Each menu As ServiceMenu.ElementMenu In res.Menus(0).ElementsMenu
                    'Création de la tile
                    Dim divContent As New HtmlGenericControl("div")

                    Dim imgTile As HtmlGenericControl = Nothing
                    Dim lblChiffre As HtmlGenericControl = Nothing
                    If menu.PossedeIndicateur Then
                        lblChiffre = New HtmlGenericControl("div")
                        lblChiffre.InnerText = CStr(menu.ValeurIndicateur)
                        lblChiffre.Style.Add("font-size", "40px")
                        lblChiffre.Style.Add("margin-top", "20px")
                    ElseIf menu.ImageBase64 <> "" Then
                        imgTile = New HtmlGenericControl("img")
                        imgTile.Style.Add("width", "32px")
                        imgTile.Style.Add("height", "32px")
                        imgTile.Style.Add("margin-top", "20px")
                        imgTile.Attributes.Add("src", "data:image/png;base64," & menu.ImageBase64)
                    End If

                    Dim lblLibelle As New HtmlGenericControl("div")
                    lblLibelle.InnerText = menu.Libelle

                    divContent.Attributes.Add("class", "btnTuile")
                    If imgTile IsNot Nothing Then divContent.Controls.Add(imgTile)
                    If lblChiffre IsNot Nothing Then
                        divContent.Controls.Add(lblChiffre)
                    End If
                    divContent.Controls.Add(lblLibelle)

                    If menu.Url <> "" Then
                        divContent.Attributes.Add("onclick", "location.href='" & Me.Page.ResolveUrl(menu.Url) & "'")
                    ElseIf menu.Assistant <> "" Then
                        divContent.Attributes.Add("onclick", Me.Page.ClientScript.GetPostBackEventReference(CType(Me.Page, OpenDelibPage).Master.UpdatePanel, menu.Assistant))
                    End If

                    pnlMenu.Controls.Add(divContent)
                Next
            End If
        End Using

        Return libelleMenu
    End Function

    Private Sub ExecuterAssistant(codeAssistant As String)
        If codeAssistant = "" Then Return

        Using svc As New ServiceAssistant.AssistantsClient
            Dim params As New ServiceAssistant.AssistantParameters
            params.Authentication = New ServiceAssistant.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.CodeAssistant = codeAssistant

            Try
                Dim results As ServiceAssistant.OperationResult
                results = svc.LancerAssistant(params)
                If results.OK Then
                    Tools.NotifySuccess(Me.Page, "Opération effectuée avec succès.")
                Else
                    Tools.NotifyErreur(Me.Page, results.ErrorMessage)
                End If
            Catch ex As Exception
                Tools.NotifyErreur(Me.Page, ex.Message)
            End Try
        End Using
    End Sub

End Class